/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.annotation.util;

import org.balisunrise.annotation.VisualEntity;
import org.balisunrise.annotation.Editor;

/**
 * Esta classe contém os atributos para construção de um editor para uma
 * entidade.
 * @author Glauco
 */
public class EditorUtilities extends VisualEntityUtilities{

    private static final String MESSAGE = "Erro ao ler Annotation \"Editor\".";

    /**
     * O alias (nome) do panel.
     */
    public String panel;

    /**
     * PanelUtilities construido com o alias panel.
     */
    public PanelUtilities panelUtilities;

    /**
     * Lê a annotation Editor com o alias da VisualEntity.
     * @param entityClass Classe para ler.
     * @param alias alias da Editor.
     * @return a annotation Editor do alias.
     * @throws AnnotationsUtilitiesException Caso não encontre.
     */
    public static Editor readEditorAnnotation(
            Class entityClass, String alias)
            throws AnnotationsUtilitiesException {

        if (entityClass == null)
            throw new AnnotationsUtilitiesException( MESSAGE +
                    "\nParâmentro \"entityClass\" não pode ser nulo!");

        if (alias == null)
            throw new AnnotationsUtilitiesException( MESSAGE +
                    "\nParâmentro \"alias\" não pode ser nulo!");

        VisualEntity ve = readVisualEntityAnnotation(entityClass);
        for (Editor ed : ve.editors()) {
            if(ed.alias().equals(alias)){
                return ed;
            }
        }

        throw new AnnotationsUtilitiesException( MESSAGE +
                "\nAnnotation \"Editor\" não encontrada para o "
                + "nome\""+ alias +"\"!");

    }

    /**
     * Le a annotation Editor e cria um EditorUtilities a parir dos
     * dados da annotation.
     *
     * @param entityClass Classe para ler.
     * @param alias alias da Editor.
     * @return EditorUtilities
     * @throws AnnotationsUtilitiesException Caso não encontre a annotation ou
     * algum parâmetro seja nulo.
     */
    public static EditorUtilities create(Class entityClass, String alias)
            throws AnnotationsUtilitiesException {

        if (entityClass == null)
            throw new AnnotationsUtilitiesException( MESSAGE +
                    "\nParâmentro \"entityClass\" não pode ser nulo!");

        if (alias == null)
            throw new AnnotationsUtilitiesException( MESSAGE +
                    "\nParâmentro \"alias\" não pode ser nulo!");

        Editor e = readEditorAnnotation(entityClass, alias);

        EditorUtilities eu = new EditorUtilities();
        eu.setName("Editor", entityClass, alias);
        eu.entityClass = entityClass;
        eu.alias = alias;
        eu.title = e.title();
        eu.panel = e.panel();

        // cria painel
        eu.panelUtilities = PanelUtilities.create(entityClass, eu.panel);
        
        return eu;
    }
}
